#include "client.h"
#include "ui_client.h"

Client::Client(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::Client)
{
    ui->setupUi(this);
    /* 设置窗口标题 */
    this->setWindowTitle("Client");

    /* 初始化端口和IP */
    ui->ip->setText("127.0.0.1");
    ui->port->setText("8888");
    /* 初始化对象 */
    client = new QTcpSocket(this);
    /* 连接 */
    client->connectToHost(ui->ip->text(), ui->port->text().toInt());
    /* 读消息 */
    connect(client, &QTcpSocket::readyRead, [=](){
        QString string = client->readAll();
        ui->recv_buffer->append(string);
    });

    /* 发送消息 */
    connect(ui->send, &QPushButton::clicked, [=](){
        QString string = ui->send_buffer->toPlainText();
        client->write(string.toUtf8());
        ui->send_buffer->clear();
    });
}

Client::~Client()
{
    delete ui;
}
